package com.fake_Interviews.controller;

import com.fake_Interviews.Mapper.FeedbackMapper;
import com.fake_Interviews.Mapper.InterviewsMapper;
import com.fake_Interviews.Mapper.LoginMapper;
import com.fake_Interviews.module.FeedbackExample;
import com.fake_Interviews.module.Interviews;
import com.fake_Interviews.module.InterviewsExample;
import com.fake_Interviews.module.Login;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;


@RestController
@RequestMapping("/api/interviewee")
public class Interviewee_Controller {

    @Autowired
    LoginMapper loginMapper;
    @Autowired
    InterviewsMapper interviewsMapper;
    @Autowired
    FeedbackMapper feedbackMapper;
    FeedbackExample feedbackExample;
    FeedbackExample.Criteria feedbackExampleCriteria;
    InterviewsExample interviewsExample;
    InterviewsExample.Criteria criteria;

    //    获取最近的一场面试
    @GetMapping("getInterviewee")
    public ResponseEntity<Object> getInterviewee() {
        Map<String, Object> stringMap = new HashMap<>();

        interviewsExample = new InterviewsExample();
        criteria = interviewsExample.createCriteria();
        Date date = new Date();
        var oldDate = date.getTime();
        var newDate = oldDate - 2 * (60 * 60 * 1000);
        date.setTime(newDate);

//        大于现在
        criteria.andJobseekersEqualTo(getCurrentUser().getId()).andInterviewtimeGreaterThan(date);
//        根据时间判断最近将要面试的一场面试
        List<Interviews> interviewsList = interviewsMapper.selectByExample(interviewsExample);
        if (interviewsList.size() > 0) {

            List<Date> dateList = new ArrayList<>();
            for (int i = 0; i < interviewsList.size(); i++) {
                dateList.add(interviewsList.get(i).getInterviewtime());
            }
            Collections.sort(dateList);//由进到远排序
            criteria.andInterviewtimeEqualTo(dateList.get(0));
            //        符合条件的用户
            if (interviewsMapper.selectByExample(interviewsExample).size() > 0) {
                Interviews one = interviewsMapper.selectByExample(interviewsExample).get(0);
//        面试官的名字
                String MianShiGuan = loginMapper.selectByPrimaryKey(one.getInterviewer()).getUsername();
                stringMap.put("id", one.getId());
                stringMap.put("Interviewer", MianShiGuan);//面试管的名字
                stringMap.put("JobSeekers", getCurrentUser().getUsername());//求职者的名字
                stringMap.put("InterviewTime", one.getInterviewtime());
                if (one.getStatus() == null) {
                    stringMap.put("Status", "null");
                } else {
                    stringMap.put("Status", one.getStatus().toString());
                }
                return ResponseEntity.ok().header("Custom-Header", "返回").body(stringMap);
            }
        }
        return ResponseEntity.status(400).header("headr", "null").body("null");
    }

    //    获取本用户的所有面试管的反馈
    @GetMapping("getHistory")
    public ResponseEntity<Object> getHistory() {
        Map<String, Object> objectMap = new HashMap<>();
        List<Object> objectList = new ArrayList<>();

        interviewsExample = new InterviewsExample();
        criteria = interviewsExample.createCriteria();

        criteria.andJobseekersEqualTo(getCurrentUser().getId());
        List<Interviews> interviewsList = interviewsMapper.selectByExample(interviewsExample);
        for (int i = 0; i < interviewsList.size(); i++) {
            objectMap.put("Interviewer", loginMapper.selectByPrimaryKey(interviewsList.get(i).getInterviewer()).getUsername());//面试管的名字

            feedbackExample = new FeedbackExample();
            feedbackExampleCriteria = feedbackExample.createCriteria();
            if (feedbackMapper.selectByPrimaryKey(interviewsList.get(i).getId()) == null) {
                objectMap.put("InterviewTubeFeedback", "面试管还没有给反馈");
            } else {
                feedbackExampleCriteria.andInterviewsIdEqualTo(interviewsList.get(i).getId());
                objectMap.put("InterviewTubeFeedback", feedbackMapper.selectByExample(feedbackExample).get(0).getInterviewtubefeedback());//面试管的反馈
            }
            objectMap.put("InterviewTime", interviewsList.get(i).getInterviewtime());//面试时间
            Map<String, Object> stringObjectMap = new HashMap<>(objectMap);
            objectList.add(stringObjectMap);
            objectMap.clear();
        }
        return ResponseEntity.ok().body(objectList);
    }

    //    获取当前登入用户
    public Login getCurrentUser() {
//        return loginMapper.selectByPrimaryKey(1);
        return (Login) SecurityUtils.getSubject().getPrincipal();
    }


}
